import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class NaviteViewPage extends StatefulWidget {
  const NaviteViewPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return NaviteViewPageState();
  }
}

class NaviteViewPageState extends State<NaviteViewPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Platform.isAndroid ? buildAndroidView() : buildUIKitView(),
          ],
        ),
      ),
    );
  }

//代码清单9-12
  //lib/NaviteViewPage.dart
  buildAndroidView() {
    return SizedBox(
      height: 200,
      child: AndroidView(
        //设置标识
        viewType: "com.flutter_to_native_test_textview",
        //参数
        creationParams: const {
          "content": " 34erw3 ",
        },
        onPlatformViewCreated: (int id) {
          //Android 原生的 View 创建后的回调
        },
        //参数的编码方式
        creationParamsCodec: const StandardMessageCodec(),
      ),
    );
  }

  //代码清单9-17
  //lib/NaviteViewPage.dart
  /// 通过 UiKitView 来加载 iOS原生View
  buildUIKitView() {
    return const SizedBox(
      height: 200,
      child: UiKitView(
        //标识
        viewType: "com.flutter_to_native_test_textview",
        creationParams: {
          "content": "flutter 传入的文本内容",
        },
        //参数的编码方式
        creationParamsCodec: StandardMessageCodec(),
      ),
    );
  }
}
